﻿using System;
using JetBrains.Annotations;
using JetBrains.Application.Progress;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Intentions.Extensibility;
using JetBrains.TextControl;
using JetBrains.Util;
using MugenMvvmToolkit.ReSharper.Highlighting;

namespace MugenMvvmToolkit.ReSharper.ViewModels.QuickFixes
{
    [QuickFix]
    public class UnusedModelPropertyAttributeFix : QuickFixBase
    {
        #region Fields

        private readonly UnusedModelPropertyAttributeHighlighting _unusedModelProperty;

        #endregion

        #region Constructors

        public UnusedModelPropertyAttributeFix([NotNull] UnusedModelPropertyAttributeHighlighting unusedModelProperty)
        {
            if (unusedModelProperty == null) throw new ArgumentNullException("unusedModelProperty");
            _unusedModelProperty = unusedModelProperty;
        }

        #endregion

        #region Overrides of QuickFixBase

        /// <summary>
        ///     Popup menu item text
        /// </summary>
        public override string Text
        {
            get { return UiResources.FixUnusedModelPropertyAttribute; }
        }

        /// <summary>
        ///     Executes QuickFix or ContextAction. Returns post-execute method.
        /// </summary>
        /// <returns>
        ///     Action to execute after document and PSI transaction finish. Use to open TextControls, navigate caret, etc.
        /// </returns>
        protected override Action<ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            _unusedModelProperty.PropertyDeclaration.RemoveAttribute(_unusedModelProperty.Attribute);
            return null;
        }

        /// <summary>
        ///     Check if this action is available at the constructed context.
        ///     Actions could store precalculated info in <paramref name="cache" /> to share it between different actions
        /// </summary>
        /// <returns>
        ///     true if this bulb action is available, false otherwise.
        /// </returns>
        public override bool IsAvailable(IUserDataHolder cache)
        {
            return _unusedModelProperty.IsValid();
        }

        #endregion
    }
}